﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using WMM.Tapioca.Instance;
using WMM.Tapioca.Taxonomy;
using System.Text.RegularExpressions;
using WMM.Tapioca.Linkbase;

namespace WMM.Tapioca.Formula.Filters
{
    /// <summary>
    /// These filters declare aspects that would be covered if the arc(s) to the filter 
    /// have a @cover attribute that has a value of true, or uncovered with value false. 
    /// </summary>
    public class AspectCoverFilter : Filter
    {
        private List<string> aspects;

        /// <returns> Content of message. </returns>
        public List<string> Aspects
        {
            get { return aspects; }
            set { aspects = value; }
        }

        //<enumeration value="all"/>
        //<enumeration value="concept"/>
        //<enumeration value="entity-identifier"/>
        //<enumeration value="location"/>
        //<enumeration value="period"/>
        //<enumeration value="unit"/>
        //<enumeration value="complete-segment"/>
        //<enumeration value="complete-scenario"/>
        //<enumeration value="non-XDT-segment"/>
        //<enumeration value="non-XDT-scenario"/>
        //<enumeration value="dimensions"/>

        private string dimension;

        /// <returns> QName specifying the dimension, or by an expression that results in 
        /// a QName value representing a dimension. </returns>
        public string Dimension
        {
            get { return dimension; }
            set { dimension = value; }
        }

        private string excludeDimension;

        /// <returns> A dimension may be excluded from covering by this filter </returns>
        public string ExcludeDimension
        {
            get { return excludeDimension; }
            set { excludeDimension = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public AspectCoverFilter(Resource resource)
            : base(resource)
        {
            aspects = new List<string>();

            IEnumerable<XElement> aspectsX = resource.ResourceXElement.Descendants().Where(d => d.Name.LocalName.Equals("aspect"));

            foreach(XElement aspectX in aspectsX)
            {
                aspects.Add(aspectX.Value);
            }

            XElement dimensionX = resource.ResourceXElement.Descendants().Where(d => d.Parent.Name.LocalName.Equals("dimension") &&
            d.Name.LocalName.Equals("qnameExpression")).FirstOrDefault();

            if (dimensionX != null)
            {
                string expression = dimensionX.Value;
                expression = expression.TrimEnd().TrimStart();
                expression = Regex.Replace(expression, @"\s+", " ");
                dimension = EvaluateExpression(expression);                
            }

            XElement dimensionQNameX = resource.ResourceXElement.Descendants().Where(d => d.Name.LocalName.Equals("qname") &&
            d.Parent.Name.LocalName.Equals("dimension")).FirstOrDefault();

            if (dimensionQNameX != null)
            {
                string qname = dimensionQNameX.Value;
                qname = qname.TrimEnd().TrimStart();
                qname = Regex.Replace(qname, @"\s+", " ");
                dimension = qname;
            }

            XElement excludeDimensionX = resource.ResourceXElement.Descendants().Where(d => d.Parent.Name.LocalName.Equals("excludeDimension") &&
            d.Name.LocalName.Equals("qnameExpression")).FirstOrDefault();

            if (excludeDimensionX != null)
            {
                string expression = excludeDimensionX.Value;  
                expression = expression.TrimEnd().TrimStart();
                expression = Regex.Replace(expression, @"\s+", " ");
                excludeDimension = EvaluateExpression(expression);             
            }

            XElement excludeDimensionQNameX = resource.ResourceXElement.Descendants().Where(d => d.Parent.Name.LocalName.Equals("excludeDimension") &&
            d.Name.LocalName.Equals("qname")).FirstOrDefault();

            if (excludeDimensionQNameX != null)
            {
                string qname = excludeDimensionQNameX.Value;
                qname = qname.TrimEnd().TrimStart();
                qname = Regex.Replace(qname, @"\s+", " ");
                excludeDimension = qname;
            }
        }


        public override List<FactItem> EvaluateFilter(VariableSet variableSet, List<FactItem> itemsToFilter, DTS dts)
        {
            foreach (string aspect in  Aspects)
            {
                variableSet.aspectsCoveredInFilter.Add(aspect, Cover);
            }

            if (dimension != null)
            {
                variableSet.aspectIncludeDimension = dimension;
            }

            if (excludeDimension != null)
            {
                variableSet.aspectExcludeDimension = excludeDimension;
            }

            return itemsToFilter;
        }
    }
}
